package com.blithe.wgt.core;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blithe.wgt.R;
import com.blithe.wgt.listener.OnDialogClickListener;
import com.blithe.wgt.utils.ScreenTools;
import com.gyf.immersionbar.ImmersionBar;

import org.greenrobot.eventbus.EventBus;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.RawRes;
import androidx.appcompat.app.AppCompatActivity;
import cn.pedant.SweetAlert.SweetAlertDialog;
import es.dmoral.toasty.Toasty;

public abstract class BaseActivity  extends AppCompatActivity {

    protected Context mContext;
    protected SweetAlertDialog waitDialog;
    private TextView tv_title,tv_right_btn;
    private ImmersionBar mImmersionBar;
    //protected HttpManager httpManager;
    protected Bundle mBundle;
    private boolean isBound = false;
    protected Handler mHandler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch(msg.what) {
                case -3:
                    Toasty.info(mContext,msg.obj.toString()).show();
                    break;
                case -4:
                    Toasty.error(mContext,msg.obj.toString()).show();
                    break;
                default:
                   BaseActivity.this.msgProcess(msg);
            }
        }
    };
    @LayoutRes
    protected abstract @RawRes
    int getLayoutResId();
    protected void msgProcess(Message msg) {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext =this;

        mImmersionBar=ImmersionBar.with(this);
        //状态栏颜色，不写默认透明色
        mImmersionBar.statusBarColor(R.color.blue_pink_bg) .init();
       // getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        //SystemUI.fixSystemUI(this);

        if(isFullScreen()){
            ScreenTools.fullScreen(this);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        if(getIntent()!=null){
            mBundle =getIntent().getExtras();
        }
        // httpManager = new HttpManager();
        setContentView(getLayoutResId());
        initBaseView();
        initView();
        initData();
        setListener();
        requestPermissions();

    }


    protected  boolean isFullScreen(){
        return false;
    }


    private void  initBaseView(){
        RelativeLayout titleLayout = findViewById(R.id.rl_title);
        if(titleLayout!=null){
            tv_title= titleLayout.findViewById(R.id.tv_title);
            titleLayout.findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
            tv_right_btn =titleLayout.findViewById(R.id.tv_right_btn);

            tv_right_btn.setVisibility(View.GONE);
            showBackBtn();
        }
    }



    /**
     * 初始化控件
     */
    protected abstract void initView();

    /**
     * 初始化Acitivity 操作
     */
    protected abstract void initData();

    /**
     * 设置事件监听
     */
    protected abstract void setListener();

    protected void   requestPermissions(){

    }


    protected void showBackBtn(){
        if(findViewById(R.id.iv_back)!=null){
            findViewById(R.id.iv_back).setVisibility(View.VISIBLE);
        }

    }
    protected void hideBackBtn(){
        if(findViewById(R.id.iv_back)!=null){
            findViewById(R.id.iv_back).setVisibility(View.GONE);
        }

    }
    protected TextView getRightButton(){
        return tv_right_btn;
    }

    /**
     * 设置中间标题
     * @param title
     */
    protected  void setTitle(String title){
        if(tv_title!=null){
            tv_title.setText(title);
        }
    }
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus){
            ScreenTools.hideBottomUIMenu(this);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();


    }



    protected void sendMessage(int what, Object obj) {
        Message msg =this.mHandler.obtainMessage(what, obj);
        this.mHandler.sendMessage(msg);

        try {
            Thread.sleep(10L);
        } catch (InterruptedException var4) {
            var4.printStackTrace();
        }

    }

    protected void sendMessage(int what) {
        this.mHandler.sendMessage(this.mHandler.obtainMessage(what, (Object)null));

        try {
            Thread.sleep(10L);
        } catch (InterruptedException var3) {
            var3.printStackTrace();
        }

    }

    protected void showTip(String msg) {
        this.sendMessage(-3, msg);
    }
    protected void showErrorTip(String msg) {
        this.sendMessage(-4, msg);
    }


    public void jumpToPage(Class<?> glass) {
        jumpToPage(glass, null, false, 0, false);
    }

    public void jumpToPage(Class<?> glass, Bundle bundle) {
        jumpToPage(glass, bundle, false, 0, false);
    }

    public void jumpToPage(Class<?> glass, boolean isFinish) {
        jumpToPage(glass, null, false, 0, isFinish);
    }

    public void jumpToPage(Class<?> glass, Bundle bundle, boolean isFinish) {
        jumpToPage(glass, bundle, false, 0, isFinish);
    }

    public void jumpToBack(Class<?> glass, Bundle bundle, boolean isFinish) {
        jumpToPage(glass, bundle, false, 0, isFinish);
    }

    public void jumpToPage(Class<?> activityClass, Bundle bundle,
                           boolean isReturn, int requestCode, boolean isFinish) {
        if (activityClass == null) {
            return;
        }

        Intent intent = new Intent();
        intent.setClass(this, activityClass);

        if (bundle != null) {
            intent.putExtras(bundle);
        }

        if (isReturn) {
            startActivityForResult(intent, requestCode);
        } else {
            startActivity(intent);
        }

        if (isFinish) {
            finish();
        }
    }


    protected  void showWaitDialog(String msg){
        waitDialog= new SweetAlertDialog(mContext, SweetAlertDialog.PROGRESS_TYPE);
        waitDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
        waitDialog.setTitleText(msg);
        waitDialog.show();
    }
    protected void dismissWaitDialog(){
        if(waitDialog!=null){
            waitDialog.dismiss();
        }
    }
    protected void showAlertDialog(String msg){
        SweetAlertDialog alertDialog = new SweetAlertDialog(mContext, SweetAlertDialog.WARNING_TYPE);
        alertDialog.setTitleText("提示");
        alertDialog.setContentText(msg);
        alertDialog.show();
    }


    protected void showConfirmDialog(String msg, OnDialogClickListener onDialogClickListener){
        SweetAlertDialog alertDialog = new SweetAlertDialog(mContext, SweetAlertDialog.NORMAL_TYPE);
        alertDialog.setTitleText("提示");
        alertDialog.setContentText(msg);
        alertDialog.setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sweetAlertDialog) {
                sweetAlertDialog.dismiss();
                if(onDialogClickListener!=null){
                    onDialogClickListener.onCancelEvent();
                }

            }
        }).setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sweetAlertDialog) {
                sweetAlertDialog.dismiss();
                if(onDialogClickListener!=null){
                    onDialogClickListener.onConfirmEvent();
                }

            }
        });
        alertDialog.show();


    }


}
